<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>巡检员管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :inline="true" :model="query">
      <el-form-item>
        <el-input
          v-model="query.insName"
          placeholder="昵称"
          :clearable="true"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="query.insTel"
          placeholder="手机"
          :clearable="true"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="query.secName"
          placeholder="管理路段"
          :clearable="true"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadInspectors" size="mini"
          ><i class="el-icon-search"></i
        ></el-button>
      </el-form-item>
      <el-form-item>
      <el-button type="success" @click="dialog.addshow = true" size="mini"
              >添加</el-button
            >
             </el-form-item>
      <el-form-item>
        <el-row>
          <el-col :span="14">
            <el-button type="success" @click="exportExcel" size="mini">导出数据</el-button>
        
          </el-col>
           <el-col :span="4">
               <el-upload
                   class="clearfix"
                   action="http://localhost:8080/tinspector/importExcel"
                   name="file"
                   :on-progress="onExcelProgress"
                   :on-success="handleExcelSuccess"                   
                   accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                   multiple
                   :limit="1"
                   
                   >
                <el-button  type="primary" size="mini">数据导入</el-button>
               </el-upload>
         </el-col>
        </el-row>
      </el-form-item>
    </el-form>

    <el-table :data="inspectorList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column align="center" type="selection" width="30">
      </el-table-column>
      <el-table-column
        align="center"
        prop="insName"
        label="巡检员名称"
        width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="insTel"
        label="手机号码"
        width="100"
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="secName"
        label="管理路段"
        width="200"
      >
      </el-table-column>

      <el-table-column
        align="center"
        prop="percentageComplete"
        label="订单完成率(%)"
        width="120"
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="insStatus"
        label="状态"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.insStatus == 0">正常</span>
          <span v-if="scope.row.insStatus == 1">禁用</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="insUpdatedTime"
        label="修改时间"
        width="160"
      >
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="selOneInspector(scope.row.insId)"
            >编辑</el-button
          >       
         <el-popconfirm
            title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row.insId)"
          >
            <el-button slot="reference" size="mini" type="danger"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>
    <el-dialog title="添加巡检员" :visible.sync="dialog.addshow" width="30%">
      <el-form ref="addForm" :model="addForm" label-width="100px">
        <el-row>
          <el-col :span="25">
            <el-form-item label="巡检员名称" prop="insName">
              <el-input v-model="addForm.insName"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="insTel">
              <el-input v-model="addForm.insTel"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="insPwd">
              <el-input v-model="addForm.insPwd" clearable></el-input>
            </el-form-item>
            <el-form-item label="管理路段" prop="secIds">
              <el-select
                v-model="addForm.secIds"
                multiple
                placeholder="请选择"
                size="mini"
              >
                <el-option
                  v-for="item in sectionList"
                  :key="item.secId"
                  :label="item.secName"
                  :value="item.secId"
                >
                </el-option>
              </el-select>         
            </el-form-item>
            <el-form-item label="状态" prop="insStatus">
                <el-radio v-model="addForm.insStatus" label="0">正常</el-radio>
                <el-radio v-model="addForm.insStatus" label="1">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.addshow = false">取 消</el-button>
        <el-button type="primary" @click="addInspector()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改巡检员" :visible.sync="dialog.updateshow" width="30%">
      <el-form ref="addForm" :model="updateForm" label-width="100px">
        <el-row>
          <el-col :span="25">
            <el-form-item label="巡检员名称" prop="insName">
              <el-input v-model="updateForm.insName"></el-input>
            </el-form-item>
            <el-form-item label="手机号码" prop="insTel">
              <el-input v-model="updateForm.insTel"></el-input>
            </el-form-item>
            <el-form-item label="登录密码" prop="insPwd">
              <el-input v-model="updateForm.insPwd" clearable></el-input>
            </el-form-item>
            <el-form-item label="管理路段" prop="secIds">
              <el-select
                v-model="updateForm.secIds"
                multiple
                placeholder="请选择"
                size="mini"
              >
                <el-option
                  v-for="item in sectionList"
                  :key="item.secId"
                  :label="item.secName"
                  :value="item.secId"
                >
                </el-option>
              </el-select>         
            </el-form-item>
            <el-form-item label="状态" prop="insStatus">
                <el-radio v-model="updateForm.insStatus" label="0">正常</el-radio>
                <el-radio v-model="updateForm.insStatus" label="1">禁用</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateshow = false">取 消</el-button>
        <el-button type="primary" @click="updateInspector()">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      inspectorList: [],
      value1:[],
      sectionList:[],
      adminId:"",
      page: {
        pageSizes: [5, 10, 20, 50, 100], //下拉列表中的数组
        total: 0, //一共有多少条数据
      },
      query: {
        currentPage: 1, //代表当前页
        pageSize: 5, //代表每页显示多少条数据
        insName: "",
        insTel: "",
        secName: "",
      },
      addForm: {adminId:null},
      updateForm: {},
      dialog: {
        addshow: false,
        updateshow: false,
        assingRolesVisible: false,
      },
    };
  },
  methods: {
    handleExcelSuccess(res, result) {
      this.$message.success("导入成功");
      if (result.response.success) {
        this.loadInspectors();
      }
    },
    // 上传中
    onExcelProgress() {},

    selectChange() {},
    selOneInspector(insId){
      this.dialog.updateshow=true;
      this.$axios.get("tinspector/"+insId).then((res) => {
            this.updateForm = res.data.data.inspector
        
      });
    },
    addInspector() {
      this.addForm.adminId=this.adminId;      
        this.$axios.post("tinspector/add", this.addForm).then((res) => {
            if(res.data.code==200){
                this.$message.success("添加成功");
                this.dialog.addshow=false;
                this.loadInspectors();
            }
        
      });
    },
    updateInspector() {
       this.$axios.post("tinspector/update", this.updateForm).then((res) => {
            if(res.data.success==true){
                this.$message.success("修改成功");
                this.dialog.updateshow=false;
                this.loadInspectors();
            }       
      });
    },
    handleDelete(id) {
      this.$axios.delete("tinspector/"+id).then((res) => {
            if(res.data.code==200){
                this.$message.success("删除成功");               
                  this.loadInspectors();
            }       
      });
    },
    checkParticulars(uid) {
      this.$router.push({ path: "/system/particulars", query: { uid: uid } });
    },
    loadInspectors() {
      this.$axios.post("tinspector/selAllIns", this.query).then((res) => {
        this.inspectorList = res.data.data.insList;
        this.page.total = res.data.data.total;
      });
    },
    loadSections() {
      this.$axios.get("sections").then((res) => {
        this.sectionList = res.data.data.sectionList;
        
      });
    },
    handleSizeChange(val) {
      //当每页数据改变时
      this.query.pageSize = val;
      this.loadInspectors();
    },
    handleCurrentChange(val) {
      //当点击页码时
      this.query.currentPage = val;
      this.loadInspectors();
    },
    exportExcel() {
      this.$axios
        .post("tinspector/exportExcel", this.query, { responseType: "blob" })
        .then((res) => {
          let fileName = res.headers["content-disposition"].split("=")[1]; //得到后端生成的文件名
          let url = window.URL.createObjectURL(new Blob([res.data]));
          let dom = document.createElement("a");
          dom.style.display = "none";
          dom.href = url;
          dom.setAttribute("download", fileName); //指定下载后的文件名，防跳转
          document.body.appendChild(dom);
          dom.click();
          // 释放内存
          dom.parentNode.removeChild(dom);
          window.URL.revokeObjectURL(dom.href);
        })
        .catch(function (error) {
          console.log(error);
        });
    },

    updateStatus(id, status) {
      this.$axios
        .post("users/upadateStatus", {
          userId: id,
          userStatus: status,
        })
        .then((res) => {
          if (res.data.code == 200) {
            if (status == 0) {
              this.$message.success("禁用成功");
            } else {
              this.$message.success("解禁成功");
            }
            this.loadInspectors();
          }
        });
    },
  },
  mounted() {
    this.loadInspectors();
    this.loadSections();
    this.adminId = sessionStorage.getItem("adminId");
    
  },
};
</script>